﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace EasyAdmin.Web.Authorizations
{
    public class AppAuthorizationHandler : AuthorizationHandler<AppRequirement>
    {
        public AppAuthorizationHandler()
        {
           
        }

        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, AppRequirement requirement)
        {
            context.Succeed(requirement);
            //Task.CompletedTask;

            var httpContext = context.Resource as HttpContext;
            ////  await httpContext.Response.WriteAsync("异常错误");
            //// 设置响应状态码
            //httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
            //await httpContext.Response.WriteAsJsonAsync(new { });
            //context.Fail();
            //return Task.CompletedTask;
            ////await httpContext.Response.CompleteAsync();//已完成响应
         

            /*
            if (!context.User.Identity.IsAuthenticated) {          
                context.Fail();
                return Task.CompletedTask;  
            }


            if (!requirement.Permissions.Any())
            {
                context.Succeed(requirement);
                return Task.CompletedTask;
            }

            var endpoint = httpContext.GetEndpoint();//获得终节点信息
            var routeData = httpContext.GetRouteData(); //获得路由信息
            if (endpoint == null) {
                context.Fail();
                return Task.CompletedTask;            
            }
            //获得控制器和action的名称
            var controllerAction = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>() as ControllerActionDescriptor;
            if (controllerAction == null) {
                context.Fail();
                return Task.CompletedTask;            
            }
            var controllerName = controllerAction.ControllerName;
            var actionName = controllerAction.ActionName;
            var routeTemplate = controllerAction.AttributeRouteInfo?.Template;
            var httpMethod = httpContext.Request.Method; //获得请求方法

           // var HttpMethod = controllerAction.HttpMethod ?? "ANY";
          












            //var filterContext = context.Resource as AuthorizationFilterContext;
            //  var httpContext = filterContext.HttpContext;



            //var userPermissions = _dataSeeds.GetUserPermissions(context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            //foreach (var permission in requirement.Permissions)
            //{
            //    if (userPermissions.Contains(permission))
            //    {
            //        context.Succeed(requirement);
            //        break;
            //    }
            //}

            return Task.CompletedTask;
            */
        }
    }
}
